package com.yuyue.springFramework.note07.entity;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * ConfigurationCondition 的实现类，用于指定在解析还是注册bean阶段，进行 matches 判断
 */
public class ConfigurationCondition1 implements ConfigurationCondition {
    @Override
    //指定生效阶段
    public ConfigurationPhase getConfigurationPhase() {
        //指定条件在bean注册阶段，这个条件才有效
        return ConfigurationPhase.REGISTER_BEAN;
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        ConfigurableListableBeanFactory configurableListableBeanFactory = context.getBeanFactory();
        //当有Service的实体时解析和注册bean
        boolean existsService = !configurableListableBeanFactory.getBeansOfType(IService.class).isEmpty();
        return existsService;
    }
}
